package com.wangshouyu.mp.pay.unified;

import com.wangshouyu.mp.pay.order.OrderSync;
import com.wangshouyu.mp.pay.scene.H5SceneType;
import com.wangshouyu.mp.pay.v3.common.enums.TransactionsType;
import com.wangshouyu.mp.pay.v3.transactions.*;

import static com.wangshouyu.mp.pay.v3.common.enums.TransactionsType.*;

/**
 * @author 王守钰
 * @date 2020年12月19日 21:42
 * @description 订单统一下单
 */
public class OrderUnifiedUtils {

    private OrderUnifiedUtils() {
    }

    /**
     * 统一下单
     * @param transactionsType 交易类型
     * @param order 订单信息
     * @param orderSync 订单同步器
     * @return 下单结果
     */
    public static TransactionsResponse unified(TransactionsType transactionsType,
                                               OrderRequest order,
                                               OrderSync orderSync){
        TransactionsRequest request = new TransactionsRequest<>();
        request.setMchId(order.getMchId());
        request.setMchSerialNo(order.getMchSerialNo());
        request.setPrivateKey(order.getPrivateKey());
        switch (transactionsType){
            case JS_API:
            case MICRO:
                request.setTransactions(jsApiUnified(request, order));
                break;
            case H5:
                request.setTransactions(h5Unified(request, order));
                break;
            case NATIVE:
                request.setTransactions( new NativeTransactionsRequest());
                request.setType(NATIVE);
                break;
            default:
                break;
        }
        // 执行统一下单
        return unified(request, order, orderSync);
    }

    /**
     * h5 统一下单
     * @param request 请求
     * @param order 订单
     * @return h5下单信息
     */
    protected static H5TransactionsRequest h5Unified(TransactionsRequest request, OrderRequest order) {
        // 交易场景
        TransactionsSceneInfo sceneInfo = new TransactionsSceneInfo();
        // 客户端ip
        sceneInfo.setPayerClientIp(order.getClientIp());
        // 构建h5支付场景
        TransactionsH5Info h5SceneInfo = new TransactionsH5Info();
        h5SceneInfo.setType(H5SceneType.WAP.getType());
        h5SceneInfo.setAppName(order.getH5AppName());
        h5SceneInfo.setAppUrl(order.getH5AppName());
        sceneInfo.setH5Info(h5SceneInfo);
        sceneInfo.setDeviceId(order.getH5DeviceId());
        // h5支付请求
        H5TransactionsRequest transactionsRequest = new H5TransactionsRequest();
        // h5支付场景信息
        transactionsRequest.setSceneInfo(sceneInfo);
        // 支付类型为h5
        request.setType(H5);
        return transactionsRequest;
    }

    /**
     * js 下单
     * @param request 请求
     * @param order 订单
     * @return 下单结果
     */
    protected static JsApiTransactionsRequest jsApiUnified(TransactionsRequest request, OrderRequest order){
        // jsApi支付请求
        JsApiTransactionsRequest transactionsRequest = new JsApiTransactionsRequest();
        transactionsRequest.setPayer(new TransactionPayer(order.getOpenId()));
        request.setType(JS_API);
        return transactionsRequest;
    }

    /**
     * 统一下单
     * @param request 订单请求
     * @param order 订单
     * @return 交易相应结果
     */
    protected static TransactionsResponse unified( TransactionsRequest<? extends BaseTransactionsRequest> request,
                                          OrderRequest order,
                                                   OrderSync orderSync) {
        BaseTransactionsRequest transactions = request.getTransactions();
        transactions.setAmount(new TransactionsAmount(order.getAmount()));
        transactions.setAppId(order.getAppId());
        transactions.setMchId(order.getMchId());
        transactions.setDescription(order.getOrderDescription());
        transactions.setOutTradeNo(order.getOrderNo());
        transactions.setNotifyUrl(order.getNotifyUrl());
        TransactionsResponse response = TransactionsUtils.transactions(request);
        // 同步订单编号
        orderSync.unified(order.getOrderNo(), response);
        return response;
    }
}
